﻿using AutoMapper;
using SheepStorage.Domain;
using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Repository.Interface;
using SheepStorage.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SheepStorage.Service.DataImp
{
    public class SheepDeathService : ISheepDeathService
    {
        private readonly IBaseRepository<SheepDeath> _sheepDeathRepository;
        IBaseRepository<BuildingManagement> _bmres;
        IBaseRepository<SheepField> _sfres;

        private readonly SheepDbContext _db;
        private readonly IMapper _mapper;

        public SheepDeathService(IBaseRepository<SheepDeath> sheepDeathRepository, SheepDbContext db, IMapper mapper, IBaseRepository<BuildingManagement> bmres, IBaseRepository<SheepField> sfres)
        {
            _sheepDeathRepository = sheepDeathRepository;
            _db = db;
            _mapper = mapper;
            _bmres = bmres;
            _sfres = sfres;
        }
        /// <summary>
        /// 死亡列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sheepSaleType"></param>
        /// <returns></returns>
        public async Task<PageOutput<SheepDeathOutput>> GetSheepDeaths(int pageIndex, int pageSize, string? sDate,string? eDate,string? overBit, int sheepDeathReason)
        {
            var list = (from sd in _sheepDeathRepository.GetAll().Where(c => c.IsDel == 0)
                        select new SheepDeathOutput()
                        {
                            SheepDeathId = sd.SheepDeathId,
                            SheepDeathDate = sd.SheepDeathDate,
                            SheepSaleType = sd.SheepSaleType,
                            OverBit = sd.OverBit,
                            ElEarTag = sd.ElEarTag,
                            SheepDeathReason = sd.SheepDeathReason,
                            SheepDeatchBreed= sd.SheepDeatchBreed,
                            SheepDeathSex = sd.SheepDeathSex,
                            BuildingManagementId= _bmres.GetAll().FirstOrDefault(x => x.BuildingManagementId == sd.BuildingManagementId)!.BuildingName,
                            SheepFieldId= _sfres.GetAll().FirstOrDefault(x => x.SheepFieldId == sd.SheepFieldId)!.SheepFieldName,
                            ReproductiveState = sd.ReproductiveState,
                            PurchaseSerialNumber = sd.PurchaseSerialNumber,
                            DocumentNumber = sd.DocumentNumber,
                            SheepBirthDate= sd.SheepBirthDate,
                            AddName= sd.AddName,
                            UpdateName= sd.UpdateName,
                            DelName= sd.DelName,
                            AddTime= sd.AddTime,
                            UpdateTime= sd.UpdateTime,
                            DelTime= sd.DelTime,
                            Remarks= sd.Remarks,
                            IsDel= sd.IsDel,

                        });
            if (!string.IsNullOrEmpty(sDate))
            {
                list = list.Where(c => c.SheepDeathDate >= DateTime.Parse(sDate));
            }
            if (!string.IsNullOrEmpty(eDate))
            {
                list = list.Where(c => c.SheepDeathDate < DateTime.Parse(eDate));
            }
            if (!string.IsNullOrEmpty(overBit))
            {
                list = list.Where(c => c.OverBit.Contains(overBit));
            }
            if (sheepDeathReason != -1)
            {
                list = list.Where(c => c.SheepDeathReason == sheepDeathReason);
            }
            //分页对象
            var result = new PageOutput<SheepDeathOutput>()
            {
                Total = list.Count(),
                PageSize = pageSize,
            };
            //分页
            var elist = list.OrderByDescending(c=>c.SheepDeathId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            result.Data = elist;
            return result;
        }
        /// <summary>
        /// 死亡添加
        /// </summary>
        /// <param name="sheepDeath"></param>
        /// <returns></returns>
        public async Task<int> AddSheepDeaths(SheepDeath sheepDeath)
        {
            var result = new SheepDeath()
            {
                SheepDeathDate = sheepDeath.SheepDeathDate,
                SheepSaleType = sheepDeath.SheepSaleType,
                DocumentNumber = sheepDeath.DocumentNumber,
                PurchaseSerialNumber = sheepDeath.PurchaseSerialNumber,
                ElEarTag = sheepDeath.ElEarTag,
                SheepDeathReason = sheepDeath.SheepDeathReason,
                ReproductiveState = sheepDeath.ReproductiveState,
                SheepDeatchBreed = sheepDeath.SheepDeatchBreed,
                SheepDeathSex = sheepDeath.SheepDeathSex,
                BuildingManagementId = sheepDeath.BuildingManagementId,
                SheepFieldId = sheepDeath.SheepFieldId,
                Remarks = sheepDeath.Remarks,
            };
            return _sheepDeathRepository.Add(result);
        }
        /// <summary>
        /// 死亡删除
        /// </summary>
        /// <param name="sheepDeathId"></param>
        /// <returns></returns>
        public async Task<int> DelSheepDeaths(int sheepDeathId)
        {
            var list = _sheepDeathRepository.GetInfoBy(sheepDeathId);
            list.IsDel = 1;
            return _sheepDeathRepository.Update(list);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="sheepDeathIds"></param>
        /// <returns></returns>
        public async Task<int> BatchDelSheepDeaths(List<int> sheepDeathIds)
        {
            foreach (var id in sheepDeathIds)
            {
                var list = _sheepDeathRepository.GetInfoBy(id);
                list.IsDel = 1;
                _sheepDeathRepository.Update(list);
            }
            return 1;
        }
        /// <summary>
        /// 反填死亡信息
        /// </summary>
        /// <param name="sheepDeathId"></param>
        /// <returns></returns>
        public async Task<SheepDeath> GetSheepDeath(int sheepDeathId)
        {
            var list = _sheepDeathRepository.GetInfoBy(sheepDeathId);
            return list;
        }
        /// <summary>
        /// 修改死亡信息
        /// </summary>
        /// <param name="sheepDeath"></param>
        /// <returns></returns>
        public async Task<int> UpdSheepDeaths(SheepDeath sheepDeath)
        {
            var list = _sheepDeathRepository.GetInfoBy(sheepDeath.SheepDeathId);
            list.SheepDeathDate = sheepDeath.SheepDeathDate;
            list.SheepSaleType = sheepDeath.SheepSaleType;
            list.DocumentNumber = sheepDeath.DocumentNumber;
            list.PurchaseSerialNumber = sheepDeath.PurchaseSerialNumber;
            list.ElEarTag = sheepDeath.ElEarTag;
            list.SheepDeathReason = sheepDeath.SheepDeathReason;
            list.ReproductiveState = sheepDeath.ReproductiveState;
            list.SheepDeatchBreed = sheepDeath.SheepDeatchBreed;
            list.SheepDeathSex = sheepDeath.SheepDeathSex;
            list.BuildingManagementId = sheepDeath.BuildingManagementId;
            list.SheepFieldId = sheepDeath.SheepFieldId;
            list.Remarks = sheepDeath.Remarks;

            return _sheepDeathRepository.Update(list);
        }
    }
}
